package org.apache.mina.protocol.dns;

import java.net.InetAddress;
import java.util.List;

import org.apache.mina.common.DefaultIoFuture;
import org.apache.mina.common.IoFutureListener;

/**
 * 
 * @author The Apache MINA Project (dev@mina.apache.org)
 * @version $Rev: 84 $, $Date: 2008-02-22 16:33:48 +0000 (Fri, 22 Feb 2008) $
 */
class DefaultDnsFuture extends DefaultIoFuture implements DnsFuture {

    private final String hostName;

    private volatile boolean nameError = false;

    DefaultDnsFuture(String hostName) {
        super(null);
        this.hostName = hostName;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.DnsFuture#getHostname()
     */
    public String getHostname() {
        return hostName;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.DnsFuture#isNameError()
     */
    public boolean isNameError() {
        return nameError;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.DnsFuture#get()
     */
    @SuppressWarnings("unchecked")
    public InetAddress get() {
        if (getValue() != null)
            return ((List<InetAddress>) getValue()).get(0);
        else
            return null;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.DnsFuture#getAll()
     */
    @SuppressWarnings("unchecked")
    public List<InetAddress> getAll() {
        return (List<InetAddress>) getValue();
    }

    /**
     * Set the DNS value and mark the future as completed. 
     * 
     * This call will also trigger the notification of subscribed
     * listeners.
     * 
     * @param addresses the list of resolved addreses
     */
    protected void setValue(List<InetAddress> addresses) {
        super.setValue(addresses);
    }

    /**
     * 
     */
    protected void setNameError() {
        nameError = true;
        super.setValue(null);
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.DefaultIoFuture#await()
     */
    @Override
    public DnsFuture await() throws InterruptedException {
        return (DnsFuture) super.await();
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.DefaultIoFuture#awaitUninterruptibly()
     */
    @Override
    public DnsFuture awaitUninterruptibly() {
        return (DnsFuture) super.awaitUninterruptibly();
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.DefaultIoFuture#addListener(org.apache.mina.common.IoFutureListener)
     */
    @Override
    public DnsFuture addListener(IoFutureListener<?> listener) {
        return (DnsFuture) super.addListener(listener);
    }

    /* (non-Javadoc)
     * @see org.apache.mina.common.DefaultIoFuture#removeListener(org.apache.mina.common.IoFutureListener)
     */
    @Override
    public DnsFuture removeListener(IoFutureListener<?> listener) {
        return (DnsFuture) super.removeListener(listener);
    }
}
